﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using Gollie.Database;
using Gollie.Payment.Targetpay;

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;

using DrawingImage = System.Drawing.Image;

using MySQL.Db;
using System.Xml.Linq;

namespace Gollie
{
	enum ReferrerType
	{
		BookMark,
		SameDomain,
		OtherDomain,
		Empty
	}

    public partial class Default : BaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
			//QueryArguments args = new QueryArguments();
			//args.Add("p_username", "tsagld");
			//args.Add("p_userid", (uint?)null);
			//args.Add("p_hastobeactivated", true);

			//Query query = new Query("sp_getuser", args);

			//ResultSet r = (ResultSet)query.Execute();

			//XDocument doc = r.ToXml();

			//this.CreateQRCode();

			//BankWire wire = new BankWire("57429", "test betaling", 10400, "http://www.gollie.nl/wirereport.aspx", "http://www.gollie.nl/wirereturn.aspx");
			//Gollie.DataTables.ActiveOffers o = new DataTables.ActiveOffers();
			//Gollie.Mail.MailSender.SendAll();
           //// string hashedUser = Queries.RegisterUser("test3", 4, "Een111", "e.goldstein@quicknet.nl", "E.", "", "Goldstein", true, "Hoofddorp", new DateTime(1967, 9, 1), "0620231338", 1);

           // SmtpClient smtpClient = new SmtpClient();

           // MailMessage msg = new MailMessage(
           //             new MailAddress("e.goldstein@quicknet.nl", "Gollie admin"),
           //             new MailAddress("eric@golliedevelop.nl")
           //             );

           // msg.Subject = "test";
           // msg.Body = "Dit is een test<br/>dus";
           // msg.SubjectEncoding = System.Text.Encoding.UTF8;
           // msg.BodyEncoding = System.Text.Encoding.UTF8;
           // msg.IsBodyHtml = true;

           // smtpClient.Send(msg);
        }

		//private void CreateQRCode()
		//{

		//	string text = "https://www.gollie.nl";


		//	QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.H);
		//	QrCode code = encoder.Encode(text);
		//	GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);

		//	int logoMargin = 6;
		//	int qrBorderWidth = 10;
		//	int extraHeight = 2 * logoMargin - qrBorderWidth;

		//	MemoryStream memStream = new MemoryStream();

		//	renderer.WriteToStream(code.Matrix, ImageFormat.Png, memStream);
		//	DrawingImage img = DrawingImage.FromStream(memStream);

		//	DrawingImage logo = DrawingImage.FromFile(Server.MapPath("/Images/LogoSmall.png"));

		//	Bitmap newImage = new Bitmap(img.Width, img.Height + logo.Height + extraHeight);
		//	Graphics g = Graphics.FromImage(newImage);
		//	g.DrawImage(img, 0, 0);

		//	RectangleF drawRect = new RectangleF(0, img.Height , img.Width, logo.Height + extraHeight);
		//	g.FillRectangle(Brushes.White, drawRect);

		//	g.DrawImage(logo, (img.Width - logo.Width) / 2, img.Height + extraHeight / 2);

		//	//Font f = new Font("Arial", 18);
		//	//StringFormat format = new StringFormat() { Alignment = StringAlignment.Center };
		//	//g.DrawString("gollie", f, Brushes.Black, drawRect, format);

		//	newImage.Save("d:\\tmp\\qrtest.png", ImageFormat.Png);

		//	g.Dispose();
		//	img.Dispose();
		//	newImage.Dispose();
		//	logo.Dispose();
		//	//using (FileStream stream = new FileStream("d:\\tmp\\qrtest.png", FileMode.Create))
		//	//{
		//	//	renderer.WriteToStream(code.Matrix, ImageFormat.Png, stream);
		//	//}

		//	//			this.hplQRCode.ImageUrl = "/ShowQR.aspx?n=" + number;
		//	//			this.hplQRCode.NavigateUrl = this.hplQRCode.ImageUrl;
		//}

		protected override void LogVisit()
		{
			base.LogVisit();

			if (HttpContext.Current != null)
			{
				if (HttpContext.Current.Request != null)
				{
					ReferrerType refType = ReferrerType.Empty;

					Uri uri = HttpContext.Current.Request.UrlReferrer;
					string referrerUrl = "";

					if (uri == null)
						refType = ReferrerType.BookMark;
					else
					{
						referrerUrl = uri.OriginalString.ToLower();

						if (referrerUrl.Contains("gollie") || referrerUrl.Contains("localhost"))
							refType = ReferrerType.SameDomain;
						else
							refType = ReferrerType.OtherDomain;
					}

					if (refType == ReferrerType.BookMark || refType == ReferrerType.OtherDomain)
					{
						logger.Info("Homepage visited through: {0}", (refType == ReferrerType.OtherDomain ? referrerUrl : "Bookmark / direct entry"));
					}
					else if (refType == ReferrerType.Empty)
						logger.Info("Homepage: empty referrer");
				}
			}
		}
    }

	
}